/*******************************************************************************
** 文件名称：page_disp_time.c
** 文件作用：显示时间页面
** 编写作者：Tom Free 付瑞彪
** 编写时间：2021-05-30
** 文件备注：
** 更新记录：
**           2021-05-30 -> 创建文件
**                                                            <Tom Free 付瑞彪>
**
**           Copyright (c) 2018-2021 付瑞彪 All Rights Reserved
**
**       1 Tab == 4 Spaces     UTF-8     ANSI C Language(C99)
*******************************************************************************/

#include "app_inc.h"

/* 页面本地参数结构 */
typedef struct
{
    gm_time_t curr_time;        /* 当前时间 */
    gm_time_t last_time;        /* 上一次时间 */
    uint8_t   cnt;              /* 内部计数器 */
    uint8_t   bg_led_cnt;       /* 背光LED计时器，超过5S自动熄灭 */
    uint8_t   alarm_cnt;        /* 闹钟计时器 */
} page_local_args_t;

/* 本地参数 */
static page_local_args_t local_args;

/* 进入页面回调 */
static void page_time_disp_into_callback(page_id_t id)
{
    /* 读取时间 */
    ds3231_read_time(&local_args.curr_time);
    /* 备份时间 */
    local_args.last_time = local_args.curr_time;
    /* 显示时间 */
    led_write_time(&local_args.curr_time);
    /* 清除计数器 */
    local_args.cnt = 0;
    /* 计时5S */
    local_args.bg_led_cnt = 10;
    /* 闹钟清除 */
    local_args.alarm_cnt = 0;
    /* 打开背光 */
    led_bg_enable();
}

/* 事件处理回调 */
static void page_time_disp_exec_callback(page_id_t id, page_event_t evt)
{
    uint8_t value;

    switch (evt)
    {
    case PAGE_EVENT_TIMER_100MS:
        if (++local_args.cnt >= 5)
        {
            local_args.cnt = 0;
            /* 读取时间 */
            ds3231_read_time(&local_args.curr_time);
            /* 查看是否有变化 */
            if ((local_args.last_time.hour != local_args.curr_time.hour) ||
                (local_args.last_time.minute != local_args.curr_time.minute) ||
                (local_args.last_time.second != local_args.curr_time.second))
            {
                /* 备份时间 */
                local_args.last_time = local_args.curr_time;
                /* 显示时间 */
                led_write_time(&local_args.curr_time);
                /* 闹钟 */
                if ((eeprom_variable.alarm_status) &&
                    (local_args.curr_time.hour == eeprom_variable.alarm_time.hour) &&
                    (local_args.curr_time.minute == eeprom_variable.alarm_time.minute) &&
                    (local_args.curr_time.second == eeprom_variable.alarm_time.second))
                {
                    local_args.alarm_cnt = 120;
                    bell_play(5, 4);
                }
                /* 整点报时 */
                else if (((local_args.curr_time.minute == 0) && (local_args.curr_time.second < 3)) ||
                         ((local_args.curr_time.minute == 30) && (local_args.curr_time.second < 2)))
                {
                    bell_play(10, 2);
                }
            }
            /* 背光计时 */
            if (local_args.bg_led_cnt)
            {
                local_args.bg_led_cnt--;
                if (!local_args.bg_led_cnt)
                {
                    /* 关闭背光 */
                    led_bg_disable();
                }
            }
            /* 闹钟计时 */
            if (local_args.alarm_cnt)
            {
                local_args.alarm_cnt--;
                if (!(local_args.alarm_cnt % 2))
                {
                    bell_play(5, 4);
                }
            }
        }
        break;

    case PAGE_EVENT_BTN_MODE_LONG_PRESS:
        if (local_args.alarm_cnt)
        {
            local_args.alarm_cnt = 0;
            bell_stop();
            break;
        }
        /* 切回到设置时 */
        page_mgr_switch(PAGE_SET_TIME_HOUR);
        bell_play(50, 1);
        break;

    case PAGE_EVENT_BTN_MODE_CLICK:
        if (local_args.alarm_cnt)
        {
            local_args.alarm_cnt = 0;
            bell_stop();
            break;
        }
        /* 开背光 */
        if (!led_bg_out_status())
        {
            led_bg_enable();
        }
        local_args.bg_led_cnt = 10;
        bell_play(5, 1);
        break;

    case PAGE_EVENT_BTN_INC_CLICK:
        if (local_args.alarm_cnt)
        {
            local_args.alarm_cnt = 0;
            bell_stop();
            break;
        }
        /* 开背光 */
        if (!led_bg_out_status())
        {
            led_bg_enable();
        }
        local_args.bg_led_cnt = 10;
        value = eeprom_variable.bg_brightness;
        if (value < 11)
        {
            value++;
            led_set_bg_brightness(value);
            eeprom_variable_set_bg_brightness(value);
        }
        bell_play(5, 1);
        break;

    case PAGE_EVENT_BTN_DEC_CLICK:
        if (local_args.alarm_cnt)
        {
            local_args.alarm_cnt = 0;
            bell_stop();
            break;
        }
        /* 开背光 */
        if (!led_bg_out_status())
        {
            led_bg_enable();
        }
        local_args.bg_led_cnt = 10;
        value = eeprom_variable.bg_brightness;
        if (value > 1)
        {
            value--;
            led_set_bg_brightness(value);
            eeprom_variable_set_bg_brightness(value);
        }
        bell_play(5, 1);
        break;

    case PAGE_EVENT_BTN_INC_LONG_PRESS:
        /* 切回到设置闹钟 */
        page_mgr_switch(PAGE_SET_ALARM_HOUR);
        bell_play(50, 1);
        break;

    case PAGE_EVENT_BTN_DEC_LONG_PRESS:
        eeprom_variable_set_alarm_status(false);
        bell_play(50, 1);
        break;

    default:
        break;
    }
}

/* 导出页面 */
PAGE_EXPORT(time_disp,
            .into_cb = page_time_disp_into_callback,
            .exec_cb = page_time_disp_exec_callback,
            .poll_cb = NULL,
            .exit_cb = NULL);
